<template>
    <div class="meal-list" v-if="type==111">
        <ul class="meal">
          <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
          <router-link :to="'/detail?id='+item.data.id" tag="li" class="liItem" v-for="item in list" :key="item.data.id">
            <div class="m-img"><div class="img" :style="{'background-image':'url(https://pic.ecook.cn/web/'+item.data.imageid+'.jpg!wl280)'}" v-lazy:background-image="'https://pic.ecook.cn/web/'+item.data.imageid+'.jpg!wl280'"></div></div>
            <h4>{{item.data.name}}</h4>
            <div class="info">
              <div class="p-img">
                <img :src="'https://pic.ecook.cn/web/'+item.authorImage+'.jpg!wl280'" alt="" /><span class="one-txt-cut">{{item.data.editname}}</span>
              </div>
              <span>{{item.collectionNumber}}人收藏</span>
            </div>
          </router-link>
          </van-list>
          </van-pull-refresh>
        </ul>
    </div>
    <div class="meal-list" v-else>
        <ul class="meal">
          <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
          <router-link :to="'/detail?id='+item.id" tag="li" class="liItem" v-for="item in list" :key="item.id" >
            <div class="m-img">
              <div class="img" v-if="item.authorid!='123564'" :style="{'background-image':'url(https://pic.ecook.cn/web/'+item.imageid+'.jpg!wl280)'}" v-lazy:background-image="'https://pic.ecook.cn/web/'+item.imageid+'.jpg!wl280'"></div>
              <div class="img" v-else :style="{'background-image':'url(blob:http://localhost:8080/'+item.imageid+')'}" v-lazy:background-image="'blob:http://localhost:8080/'+item.imageid+''"></div>
            </div>
            <h4>{{item.name}}</h4>
            <div class="info">
              <div class="p-img" v-if="item.user">
                <img :src="'https://pic.ecook.cn/web/'+item.user.imageid+'.jpg!s1'" alt="" /><span class="one-txt-cut">{{item.user.nickname}}</span>
              </div>
              <div class="p-img" v-else>
                <img :src="'https://pic.ecook.cn/web/'+item.authorimageid+'.jpg!s1'" alt="" /><span class="one-txt-cut">{{item.authorname}}</span>
              </div>
              <span v-if="!item.infos">{{item.collectCount}}人收藏</span>
              <span v-else>{{item.infos.collectionCount}}人收藏</span>
            </div>
          </router-link>
          </van-list>
          </van-pull-refresh>
        </ul>
    </div>
</template>

<script>
    export default {
        props:['HomeList'],
        data() {
          return {
            lists: null,
            type:111,
            list: [],
            loading: false,
            finished: false,
            refreshing: false,
            num:0,
          };
        },
        methods: {
          onLoad() {
            setTimeout(() => {
              if (this.refreshing) {
                this.list = [];
                this.refreshing = false;
                this.num = 0;
              }
              
              for (let i = this.num; i < this.num + 10; i++) {
                  if (this.lists[i]) {
                    this.list.push(this.lists[i]);
                  }
              }
              this.num += 10
              this.loading = false;

              if (this.list.length >= this.HomeList.length) {
                this.finished = true;
              }
            }, 1000);
        },
        onRefresh() {
            // 清空列表数据
            this.finished = false;

            // 重新加载数据
            // 将 loading 设置为 true，表示处于加载状态
            this.loading = true;
            this.onLoad();
        },
        },
        created() {
          this.type = window.localStorage.getItem('id')==null?111:window.localStorage.getItem('id')
          this.lists = this.HomeList
          // console.log(this.HomeList);
        },
        // watch:{
        //   "type":function(oldvalue,value){
        //     console.log(oldvalue,value);
        //     if (Number(oldvalue)==value) {
        //       console.log("相等");
        //       for (let i = 0; i < 10; i++) {
        //           this.list.push(this.lists[i]);
        //       }
        //     }
        //   }
        // }
    }
</script>

<style lang="less" scoped>
.meal-list{
    width: 100%;
    margin-top: 5px;
    padding-bottom: 55px;

        .meal {
          width: 90%;
          margin: 0 auto;
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;
          // margin-bottom: 55px;

          .liItem {
            width: 48%;
            margin-bottom: 15px;

            .m-img {
              width: 100%;
              height: 188px;
              border-radius: 8px;
              overflow: hidden;

              .img{
                width: 100%;
                height: 100%;
                background-repeat: no-repeat;
                background-size: cover;
                background-position: center center;
              }
            }

            & > h4 {
              margin: 8px 0;
              color: #333;
              font-weight: 600;
              font-size: 14px;
              line-height: 22px;
            }

            .info {
              display: flex;
              justify-content: space-between;
              align-items: center;

              .p-img {
                width: 50%;
                display: flex;
                align-items: center;
                font-size: 12px;

                & > img {
                  width: 18px;
                  height: 17px;
                  display: block;
                  margin-right: 5px;
                  border-radius: 50%;
                }
              }

              & > span {
                text-align: right;
                width: 50%;
                display: block;
                font-size: 12px;
                color: #999;
                
              }
            }
          }
        }

}
  .meal-leave-to {
      opacity: 0;
      transform: translateX(150px);
  }
  .meal-enter {
      opacity: 0;
      transform: translateX(-150pX);
  }
  .meal-leave-active,
  .meal-enter-active {
      transition: all 0.3s linear;
  }
::v-deep .van-pull-refresh{
  width: 100%;
}
::v-deep .van-list{
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
::v-deep .van-list__finished-text{
  width: 100%;
  text-align: center;
}
::v-deep .van-list__loading{
  width: 100%;
  text-align: center;
}
::v-deep .van-loading{
  width: 100%;
  text-align: center;
}
</style>